# 爬厦门2022年的天气

url = "https://tianqi.2345.com/Pc/GetHistory"



header = {
    "user-agent":"""Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"""
}

import requests
import pandas as pd

def craw_table(year, month):
    """提供年份，月份爬数据"""
    params = {
        "areaInfo[areaId]": 71920,
        "areaInfo[areaType]": 2,
        "date[year]": year,
        "date[month]": month,
    }
    r = requests.get(url, headers=header, params=params)
    # print(r.text) # {"code":1,"msg":"","data":"<ul class=\"history-msg\">\n    <li>\u5e73\u5747\u9ad8\u6e29\uff1a<em class=\"orange-txt\">22\u00b0<\/em
    # 取json 数据
    data = r.json()["data"]

    # pandas 解析html
    df = pd.read_html(data)[0]
    return df

df_list = []
# 爬取2018年到2022年厦门的天气数据
for year in range(2018,2023):
    for month in range(1,13):
        print()
        df = craw_table(2021, 3)
        df_list.append(df)

pd.concat(df_list).to_excel("厦门2018-2022天气数据.xlsx", index=False)